$(document).ready(function() {
    var valDias = 1;
    var valClones = 1;
    var valBoxes = new Array();
    $('#sem').bind('change', function() {
        var sem = $(this).find("option:selected").val();
        var curso = $(this).data('curso');
        $.get('ajaxRequest', {sem: sem, curso: curso}, function(data) {
            $('#disc').html(data);
            $('#grade').show();
        });
    });
    $('#disc').bind('change', function() {
        $('#addCampos').show();
    });
    $('#dias').bind('change', function() {
        var dias = $(this).find("option:selected").val();
        var disc = $('#disc').find("option:selected").val();
        valDias = dias;
        valClones = parseInt(dias);
        //console.log(valDias);
        //console.log(valClones);
        $("#selectHora").html('');
        var spans = '';
        for (var i = 1; i <= valClones; i++) {
            var span2 = '<div class="span4" id="formHora-' + i + '">' +
                    '<div class="control-group">' +
                    '<label class="control-label" for="sem">Selecione a hora do dia ' + i + '</label>' +
                    '<div class="controls">' +
                    '<select id="horaSel-' + i + '" name="horaSel-' + i + '" class="input-xlarge">' +
                    '<option>Selecione</option>' +
                    '<option name="hora' + i + '" value="7:30h">7:30h</option>' +
                    '<option name="hora' + i + '" value="8:30h">8:30h</option>' +
                    '<option name="hora' + i + '" value="9:30h">9:30h</option>' +
                    '<option name="hora' + i + '" value="10:30h">10:30h</option>' +
                    '<option name="hora' + i + '" value="11:30h">11:30h</option>' +
                    '<option name="hora' + i + '" value="13:30h">13:30h</option>' +
                    '<option name="hora' + i + '" value="14:30h">14:30h</option>' +
                    '<option name="hora' + i + '" value="15:30h">15:30h</option>' +
                    '<option name="hora' + i + '" value="16:30h">16:30h</option>' +
                    '<option name="hora' + i + '" value="17:30h">17:30h</option>' +
                    '<option name="hora' + i + '" value="18:30h">18:30h</option>' +
                    '</select>' +
                    '</div>' +
                    '</div>' +
                    '</div>';
            spans = spans + span2;
        }
        //console.log(spans);
        $('#selectHora').html(spans);
        $.get('ajaxRequest2', {dias: dias, disc: disc}, function(data) {

            $('#diaSpan').html(data);
            $('#diaWrapper').show();
            $('#continue').show();
        });
    });
    var checked = [],
            $check = $('.diabox').change(function() {
        $('#selectHora').show();
        $('#controlForm').show();
        if (this.value == -1 && this.checked) {
            $check.not(this).prop('disabled', true).prop('checked', false);
            checked = [];
        }
        else {
            $check.prop('disabled', false);
            checked.push(this);
            checked = $(checked)
            checked.prop('checked', false).slice("-" + parseInt(valDias)).prop('checked', true);
        }
    });
    $('.salvarS').click(function() {
        var ivals = {};
        var checkedBoxesArr = new Array();
        $("input[name='diabox']:checked").each(function() {
            checkedBoxesArr.push($(this).val());
        });
        var checkedBoxesStr = checkedBoxesArr.toString();
        var dataString = $("#formSolicita").serialize() +
                '&diaBoxes=' + checkedBoxesStr;
        
        ivals = dataString
        $.post("salvar.do", ivals);
        
        $("#formSolicita").html('<div class="row-fluid"><div class="alert alert-success"><h4>Disciplina solicitada com sucesso!</div></div>');
    });

});
